/**
 * 
 */
package com.mnurre.shopping.assistant;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author michael
 * Activity to modify the tax rate.
 */
public class EditTaxRateActivity extends Activity implements OnClickListener {

	private static final String TAG = "EditTaxRateActivity";
	private EditText taxRateEditText;
	private Button saveTaxRateButton;
	private TaxRateData mTaxData;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_tax_rate);
		Log.d(TAG, "Getting a reference to the UI components.");
		taxRateEditText = (EditText)findViewById(R.id.taxRateEditText);
		saveTaxRateButton = (Button)findViewById(R.id.saveTaxRateButton);
		saveTaxRateButton.setOnClickListener(this);
		
		Log.d(TAG, "Creating a new instance of TaxRateData.");
		mTaxData = new TaxRateData(this);
		mTaxData.open();
		
		Log.d(TAG, "Getting the latest tax rate and setting taxRateEditText.");
		taxRateEditText.setText(mTaxData.getRate());
		mTaxData.close();
		Log.d(TAG, "Done in onCreate.");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String rate = taxRateEditText.getText().toString();
		mTaxData.open();
		mTaxData.updateRate(rate);
		mTaxData.close();
		setResult(RESULT_OK);
		finish();
	}
}
